package com.sharecode.stock.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sharecode.stock.repository.StockDateConfigRepository;
import com.sharecode.stock.utils.DateUtils;
import com.sharecode.stock.vo.DateVo;
import com.sharecode.stock.vo.StockDateConfig;

@Service
public class StockDateConfigService {

	@Autowired
	private StockDateConfigRepository stockDateConfigRepository;

	public void save(StockDateConfig stockDateConfig) {
		stockDateConfigRepository.save(stockDateConfig);
	}

	public void save(List<StockDateConfig> stockDateConfigs) {
		stockDateConfigRepository.saveAll(stockDateConfigs);
	}

	public List<StockDateConfig> findAll() {
		return stockDateConfigRepository.findAll();
	}

//	public void init() {
//		save(getWeek());
//	}

	public static List<StockDateConfig> getWeek() {
		Date date = DateUtils.parse("2018-01-01", DateUtils.FORMAT_YYYY_MM_DD);
		List<DateVo> dateVos = new ArrayList<>();
		for (int i = 0; i < 3000; i++) {
			DateVo vo = DateUtils.day(i, date, DateUtils.FORMAT_YYYY_MM_DD);
			int week = vo.getWeek();
			if (week == 1 || week == 2) {
				dateVos.add(vo);
			}
		}
		System.out.println(dateVos.size());
		int size = dateVos.size();
		if (size % 2 == 1) {
			dateVos = dateVos.subList(0, size - 1);
		}
		System.out.println(dateVos.size());
		List<StockDateConfig> stockDateConfigs = new ArrayList<>();
		for (int i = 0; i < dateVos.size(); i++) {
			StockDateConfig stockDateConfig = new StockDateConfig();
			stockDateConfig.setStartDate(dateVos.get(i).getDate());
			i++;
			stockDateConfig.setEndDate(dateVos.get(i).getDate());
			stockDateConfigs.add(stockDateConfig);
		}
		System.out.println(stockDateConfigs.size());

		return stockDateConfigs;
	}

}
